package com.goldsign.pay.provider.user.util;

import com.goldsign.pay.common.util.Md5;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
/**
 * 登录令牌
 * @author lyp
 *
 */
public class AppLoginToken {
	private String timeFormat = "yyyy-MM-dd HH:mm:ss";
	/**生成token的算法是时间戳+username+password的md5_32**/
	private String phone;


	public AppLoginToken(String phone,String password){
		try {
			this.setPhone(phone);;
			this.tokenId= Md5.md5_32(System.currentTimeMillis()+phone+password);
			//SysLog.info("用户登录"+phone+"tokenId："+tokenId);
		} catch (Exception e) {

		}
	}


	/**登录令牌**/
	private String tokenId;

	public String getTokenId() {
		return tokenId;
	}
	public void setTokenId(String tokenId) {
		this.tokenId = tokenId;
	}

	/**上次活动时间**/
	private String lastActiveTime= new SimpleDateFormat(timeFormat).format(new Date());

	/**
	 * 用来存放用户基本信息
	 */
	private ConcurrentHashMap<String,Object> attributes=new ConcurrentHashMap<String,Object>();

	/**
	 * 自身判断是否登录超时
	 * @param expireTime
	 * @return
	 * @throws Exception
	 */
	public boolean isExpire(long expireTime)throws Exception{
		if((System.currentTimeMillis()-new SimpleDateFormat(timeFormat).parse(lastActiveTime).getTime())>expireTime){
			return true;
		}
		return false;
	}
	@Override
	public String toString() {
		return "AppLoginToken [lastActiveTime=" + lastActiveTime + ", tokenId="
				+ tokenId + "]";
	}


	public String getLastActiveTime() {
		return lastActiveTime;
	}
	public void setLastActiveTime(String lastActiveTime) {
		this.lastActiveTime = lastActiveTime;
	}
	public ConcurrentHashMap<String, Object> getAttributes() {
		return attributes;
	}
	public void setAttributes(ConcurrentHashMap<String, Object> attributes) {
		this.attributes = attributes;
	}
	public void active() {
		this.lastActiveTime = new SimpleDateFormat(timeFormat).format(new Date());
	}

	public void setAttrubute(String key, Object responseDetail) {
		attributes.put(key, responseDetail);
	}
	public Object getAttrubute(String key) {
		return attributes.get(key);
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
}
